package com.miyake.dsp.osa.lab.wrapper;

import org.apache.commons.lang.ArrayUtils;

import com.miyake.dsp.analyzer.PeakSearchCommon;
import com.miyake.dsp.analyzer.PeakSearchSetting;
import com.miyake.dsp.common.DataMerger;
import com.miyake.dsp.osa.lab.Calculator;
import com.miyake.dsp.osa.lab.LabUiAnnotation;
@LabUiAnnotation(caption="Peak Search2")
public class PeakSearch2Wrapper extends Calculator {
	public static final String INPUTX = "In-X";
	public static final String INPUTY = "In-Y";
	public static final String OUTPUTX = "Out-X";
	public static final String OUTPUTY = "Out-Y";
	public static final String OUTPUTNOISE = "Out-Noise";
	private PeakSearchSetting setting;
	
	public PeakSearch2Wrapper(PeakSearchSetting setting) {
		this.setting = setting;
		initInput(INPUTX);
		initInput(INPUTY);
		initOutput(OUTPUTX);
		initOutput(OUTPUTY);
		initOutput(OUTPUTNOISE);
	}
	
	@Override
	public void process() {
		double[] x = null;
		double[] y = null;
		
		if (getInput(INPUTY) != null) {
			y = getInput(INPUTY).getRealPart();
		}
		
		if (getInput(INPUTX) != null) {
			x = getInput(INPUTX).getRealPart();
		}
		else {
			x = new double[y.length];
			for (int i = 0; i < y.length; i++) {
				x[i] = i;
			}
		}

		
		//DataMerger peakSearch = new DataMerger(y);
		PeakSearchCommon peakSearch =new PeakSearchCommon(y);
		int[] indexes = ArrayUtils.toPrimitive(peakSearch.getPeakInexes().toArray(new Integer[0]));
		double[] out = new double[indexes.length];
		for (int i = 0; i < out.length; i++) {
			out[i] = x[indexes[i]];
		}
		setOutput(OUTPUTX, out);
		setOutput(OUTPUTNOISE, peakSearch.getNoiseLevel());
	}

	@Override
	public Object getSetting() {
		return this.setting;
	}

}
